var entityCollection = require('../../../Base/server/controllers/EntityCollection');

//获取批次
exports.getMOLotNumber = function(condition, completeFun) {
    var entityMO = entityCollection.getEntity("MO");
    entityMO.find(condition, { LotNumber: 1 }, null, function(err, records) {
        if (err) {
            completeFun(err);
        }
        completeFun(err, records);
    });
};

//根据工单号获取批次信息
exports.getMOData = function(mo, completeFun) {
    var entityMO = entityCollection.getEntity("MO");
    entityMO.findOne({
            _id: mo
        }, null,
        function(err, record) {
            if (err) {
                var newErr = new Error();
                newErr.leval = 9;
                newErr.title = '查询工单错误';
                newErr.message = '请检查查询参数！';
                completeFun(newErr);
            } else {
                completeFun(null, record);
            }
        }
    );
};
//根据工单号获取物料信息
exports.getMOItemMasterData = function(mo, completeFun) {
    var entityMO = entityCollection.getEntity("MO");
    entityMO.findOne({
            _id: mo
        }, [{ path: "ItemMaster", select: "Code Name" }],
        function(err, record) {
            if (err) {
                var newErr = new Error();
                newErr.leval = 9;
                newErr.title = '查询工单错误';
                newErr.message = '请检查查询参数！';
                completeFun(newErr);
            } else {
                completeFun(null, record);
            }
        }
    );
};

/**
 * 获取所有工单(有效 Status: 1)
 * 查询条件 已上线数量 – 已完工数量 – 已报废数量>0 and 已删除标识=false
 * @param completeFun
 */
exports.getAllMOInfo = function(completeFun) {
    var entityMO = entityCollection.getEntity("MO");
    entityMO.find({ Status: 1, $where: "this.OnlineQty - this.TotalCompleteQty > 0" }, { Code: 1 }, null, function(err, records) {
        if (err) {
            completeFun(err);
        }
        completeFun(err, records);
    });
};
